package com.xxl.shoppingapp.ui.mine;

import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xxl.baselib.baseui.widget.CircleImageView;
import com.xxl.baselib.baseui.widget.MultipleItem;
import com.xxl.baselib.util.LogUtil;
import com.xxl.baselib.util.ToastUtils;
import com.xxl.shoppingapp.ui.adapter.MultipleItemQuickAdapter;
import com.xxl.shoppingapp.R;
import com.xxl.shoppingapp.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * @author xuelian.xiong 2018/4/10
 * @Package com.xxl.shoppingapp.ui.main
 * @Title: MineFragment
 * @Description: (用一句话描述该文件做什么)
 * Copyright (c) 传化公路港物流有限公司版权所有
 * Create DateTime: 2018/4/10
 */
public class MineFragment extends BaseFragment {

    @Bind(R.id.recyclerView)
    RecyclerView baseRecyclerView;
    @Bind(R.id.swipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private MultipleItem multipleItem = null;

    private List<MultipleItem> itemDataList;

    private MultipleItemQuickAdapter multipleItemQuickAdapter;

    @Override
    protected int getLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        setTitle(getString(R.string.mine));
        initSwipeRefreshLayout();

        initItemData();

        initRecyclerView();

        initListener();
    }

    private void initSwipeRefreshLayout() {
        mSwipeRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light);

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        multipleItemQuickAdapter.notifyDataSetChanged();
                        mSwipeRefreshLayout.setRefreshing(false);
                        ToastUtils.show("刷新完成");
                    }
                }, 2000);
            }
        });
    }


    private void initItemData() {
        itemDataList = new ArrayList<>();

        multipleItem = new MultipleItem(MultipleItem.TYPE_COUNT, 5);
        multipleItem.mString1 = "收藏";
        multipleItem.mString2 = "关注";
        itemDataList.add(multipleItem);

        multipleItem = new MultipleItem(MultipleItem.TYPE_ORDER_HEADER, 5);
        multipleItem.mString2 = "type2";
        itemDataList.add(multipleItem);

        //添加待付款
        multipleItem = new MultipleItem(MultipleItem.TYPE_ORDER, 1);
        multipleItem.mString1 = "待付款";
        multipleItem.isShow = true;
        multipleItem.count = 2;
        itemDataList.add(multipleItem);
        //待发货
        multipleItem = new MultipleItem(MultipleItem.TYPE_ORDER, 1);
        multipleItem.mString1 = "待发货";
        itemDataList.add(multipleItem);
        //待收货
        multipleItem = new MultipleItem(MultipleItem.TYPE_ORDER, 1);
        multipleItem.mString1 = "待收货";
        multipleItem.isShow = true;
        multipleItem.count = 1;
        itemDataList.add(multipleItem);
        //待评价
        multipleItem = new MultipleItem(MultipleItem.TYPE_ORDER, 1);
        multipleItem.mString1 = "待评价";
        itemDataList.add(multipleItem);

        //退款/售后
        multipleItem = new MultipleItem(MultipleItem.TYPE_ORDER, 1);
        multipleItem.mString1 = "退款/售后";
        itemDataList.add(multipleItem);

        multipleItem = new MultipleItem(MultipleItem.TYPE_BALANCE, 5);
        multipleItem.mString1 = "￥893.00";
        itemDataList.add(multipleItem);

        multipleItem = new MultipleItem(MultipleItem.TYPE_TOOLS_HEADER, 5);
        multipleItem.mString1 = "type5";
        itemDataList.add(multipleItem);

            multipleItem = new MultipleItem(MultipleItem.TYPE_TOOLS, 1);
            multipleItem.mString1 = "使用帮助";
            multipleItem.isShow = true;
            multipleItem.count = 100;
            itemDataList.add(multipleItem);

        multipleItem = new MultipleItem(MultipleItem.TYPE_TOOLS, 1);
        multipleItem.mString1 = "卡券中心";
        multipleItem.isShow = true;
        multipleItem.count = 100;
        itemDataList.add(multipleItem);
        multipleItem = new MultipleItem(MultipleItem.TYPE_TOOLS, 1);
        multipleItem.mString1 = "我的评价";
        multipleItem.isShow = true;
        multipleItem.count = 100;
        itemDataList.add(multipleItem);

        multipleItem = new MultipleItem(MultipleItem.TYPE_TOOLS, 1);
        multipleItem.mString1 = "我的发票";
        multipleItem.isShow = true;
        multipleItem.count = 100;
        itemDataList.add(multipleItem);

        multipleItem = new MultipleItem(MultipleItem.TYPE_TOOLS, 1);
        multipleItem.mString1 = "每日打卡";
        multipleItem.isShow = true;
        multipleItem.count = 100;
        itemDataList.add(multipleItem);
    }

    private void initRecyclerView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 5);
        baseRecyclerView.setLayoutManager(gridLayoutManager);

        multipleItemQuickAdapter = new MultipleItemQuickAdapter(itemDataList);

        View headerView = getHeaderView(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.my_header_image:
                        ToastUtils.show("你点击了头像");
                        break;
                    case R.id.my_header_settings:
                        ToastUtils.show("你点击了设置");
                        break;
                }
            }
        });

        multipleItemQuickAdapter.addHeaderView(headerView);

        baseRecyclerView.setAdapter(multipleItemQuickAdapter);
    }


    private View getHeaderView(View.OnClickListener listener) {
        View headerView = getActivity().getLayoutInflater().inflate(R.layout.layout_my_header, (ViewGroup) baseRecyclerView.getParent(), false);

        CircleImageView myHeaderImage = (CircleImageView) headerView.findViewById(R.id.my_header_image);
        myHeaderImage.setImageResource(R.mipmap.header_image);
        myHeaderImage.setOnClickListener(listener);

        TextView myHeaderName = (TextView) headerView.findViewById(R.id.my_header_name);
        myHeaderName.setText("名字");

        TextView myHeaderMobile = (TextView) headerView.findViewById(R.id.my_header_mobile);
        myHeaderMobile.setText("手机号");

        ImageView myHeaderSettings = (ImageView) headerView.findViewById(R.id.my_header_settings);
        myHeaderSettings.setOnClickListener(listener);

        return headerView;
    }

    private void initListener() {
        multipleItemQuickAdapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
            @Override
            public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                return itemDataList.get(position).getSpanSize();
            }
        });

        multipleItemQuickAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                ToastUtils.show("第  " + position);

                //可以再加一层 类型 的判断，一般来说订单不是点了就消失的
                if (itemDataList.get(position).getItemType() == MultipleItem.TYPE_TOOLS) {
                    if (itemDataList.get(position).isShow) {
                        itemDataList.get(position).isShow = false;
                        LogUtil.xxlLog().d("count  =  " + itemDataList.get(position).count);
                        multipleItemQuickAdapter.notifyItemChanged(position + 1);
                    } else
                        itemDataList.get(position).isShow = false;
                }

            }
        });

        multipleItemQuickAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.my_favorites:
                        ToastUtils.show("收藏");
                        break;
                    case R.id.my_bands:
                        ToastUtils.show("关注");
                        break;
                    case R.id.ll_my_order:
                        ToastUtils.show("全部订单");
                        break;
                    case R.id.my_balance_btn:
                        ToastUtils.show("立即充值");
                        break;
                }
            }
        });

    }

}
